package com.bowen.spike.controller;


import com.bowen.spike.common.Result;
import com.bowen.spike.context.UserContext;
import com.bowen.spike.entity.User;
import com.bowen.spike.my_enum.Gender;
import com.bowen.spike.service.IUserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author bowen
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody User user) {
        String token = userService.login(user);
        return Result.ok(token);
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result<Void> register(@Valid @RequestBody User user) {
        userService.register(user);
        return Result.ok();
    }

    /**
     * 修改密码
     *
     * @param password
     * @return
     */
    @PutMapping("/change-password")
    public Result<Void> changePassword(@RequestParam("password") String password) {
        userService.changePassword(password);
        return Result.ok();
    }

    /**
     * 修改性别
     *
     * @param gender
     * @return
     */
    @PutMapping("/change-gender/{gender}")
    public Result<Void> changeGender(@PathVariable Integer gender) {
        userService.changeGender(Gender.of(gender));
        return Result.ok();
    }

    @DeleteMapping("/deleteAll")
    public Result<Void> deleteAll(){
        userService
                .lambdaUpdate()
                .remove();
        return Result.ok();
    }


}
